package com.lebang.service.ybd;

import com.alibaba.fastjson.JSON;
import com.lebang.component.RedisClient;
import com.lebang.domain.ybd.MemberGoldConfig;
import com.lebang.example.ybd.MemberGoldConfigExample;
import com.lebang.mapper.ybd.MemberGoldConfigMapper;
import com.lebang.service.BaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class MemberGoldService extends BaseService<MemberGoldConfig,Integer, MemberGoldConfigExample> {
    @Autowired
    private MemberGoldConfigMapper memberGoldConfigMapper;
    @Autowired
    private RedisClient redisClient;
    private static final String HASH_KEY_GOLD = "HASH_KEY_GOLD";
    private static final String GOLD_KEY = "AppGoldConfig";



    //添加缓存
    private void addCache(){


        List<MemberGoldConfig> appRunConfigs1 = this.selectByExample(null);
        redisClient.hset(HASH_KEY_GOLD,GOLD_KEY,JSON.toJSONString(appRunConfigs1));


    }
    //删除
    private void delCache(){
        String hget = redisClient.hget(HASH_KEY_GOLD, GOLD_KEY);
        //先删除缓存
        if(hget!=null){
           redisClient.hdel(HASH_KEY_GOLD,GOLD_KEY);
        }
    }

    /**
     * 查询金币可用的
     * @return
     */
    public List<MemberGoldConfig> getAllUsableGolds() {

            MemberGoldConfigExample memberGoldConfigExample = new MemberGoldConfigExample();
            memberGoldConfigExample.setOrderByClause("id");
            memberGoldConfigExample.createCriteria().andHasDeletedEqualTo(0).andEnableEqualTo(1);
            List<MemberGoldConfig> memberGoldConfigs = memberGoldConfigMapper.selectByExample(memberGoldConfigExample);

        return memberGoldConfigs;
    }

    /**
     *  修改金币配置
     * @param memberGoldConfig
     */

    public void updateGoldConfig(MemberGoldConfig memberGoldConfig) {
        this.delCache();
        this.updateById(memberGoldConfig);

        this.addCache();
    }

    public void deleteGoldConfig(Integer configId) {
        this.delCache();
        this.deleteById(configId);

        this.addCache();
    }

    public void insertGoldConfig(MemberGoldConfig memberGoldConfig) {
        this.delCache();
        this.insertModel(memberGoldConfig);

        this.addCache();
    }
}
